<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>计算步数</title>
  <style>
    div {
      margin: 150px auto 0;
      text-align: center;
      font-size: 36px;
      color: #ff0000;
    }
  </style>
</head>

<body>
  <!-- 编程题目：计算微信运动步数
  要求：已知小明2021-08-16至2021-09-15的微信运动步数数据(steps)
        请编写一个函数，将其中某一个时间段的步数进行小计
  返回结果示例：
        输入：2021-09-01,2021-09-07
        返回：59955
  -->

  <script>
    var steps = [{
        "step": 1753,
        "date": "2021-08-16"
      },
      {
        "step": 11274,
        "date": "2021-08-17"
      },
      {
        "step": 7676,
        "date": "2021-08-18"
      },
      {
        "step": 9360,
        "date": "2021-08-19"
      },
      {
        "step": 8816,
        "date": "2021-08-20"
      },
      {
        "step": 11808,
        "date": "2021-08-21"
      },
      {
        "step": 10655,
        "date": "2021-08-22"
      },
      {
        "step": 8075,
        "date": "2021-08-23"
      },
      {
        "step": 10070,
        "date": "2021-08-24"
      },
      {
        "step": 8130,
        "date": "2021-08-25"
      },
      {
        "step": 10590,
        "date": "2021-08-26"
      },
      {
        "step": 9419,
        "date": "2021-08-27"
      },
      {
        "step": 2005,
        "date": "2021-08-28"
      },
      {
        "step": 3972,
        "date": "2021-08-29"
      },
      {
        "step": 2945,
        "date": "2021-08-30"
      },
      {
        "step": 3278,
        "date": "2021-08-31"
      },
      {
        "step": 5561,
        "date": "2021-09-01"
      },
      {
        "step": 5568,
        "date": "2021-09-02"
      },
      {
        "step": 10340,
        "date": "2021-09-03"
      },
      {
        "step": 9885,
        "date": "2021-09-04"
      },
      {
        "step": 12394,
        "date": "2021-09-05"
      },
      {
        "step": 8328,
        "date": "2021-09-06"
      },
      {
        "step": 7879,
        "date": "2021-09-07"
      },
      {
        "step": 12817,
        "date": "2021-09-08"
      },
      {
        "step": 7416,
        "date": "2021-09-09"
      },
      {
        "step": 5868,
        "date": "2021-09-10"
      },
      {
        "step": 12907,
        "date": "2021-09-11"
      },
      {
        "step": 9720,
        "date": "2021-09-12"
      },
      {
        "step": 7055,
        "date": "2021-09-13"
      },
      {
        "step": 6323,
        "date": "2021-09-14"
      },
      {
        "step": 9785,
        "date": "2021-09-15"
      },
      {
        "step": 6919,
        "date": "2021-09-16"
      },
      {
        "step": 4882,
        "date": "2021-09-17"
      },
      {
        "step": 4557,
        "date": "2021-09-18"
      },
      {
        "step": 10006,
        "date": "2021-09-19"
      },
      {
        "step": 6300,
        "date": "2021-09-20"
      },
      {
        "step": 4825,
        "date": "2021-09-21"
      }
    ];

    // // 定义函数计算累计步数
    // function getSteps(t1, t2) {
    //   var sum = 0; // 步数小计初始为0
    //   var t1Index = 0; // 和t1时间对应的那个对象的索引  假设为0
    //   var t2Index = 0;
    //   // 循环遍历数组
    //   for (var i = 0; i < steps.length; i++) {
    //     // 通过循环 判断记录和时间对应的 对象索引
    //     if (steps[i].date === t1) t1Index = i;
    //     if (steps[i].date === t2) t2Index = i;
    //   }
    //   // 根据索引进行遍历steps，累计步数
    //   for (var j = t1Index; j <= t2Index; j++) {
    //     sum += steps[j].step;
    //   }
    //   // 返回小计步数
    //   return sum;
    // }

    // // var res = getSteps('2021-09-01','2021-09-07');
    // var res = getSteps('2021-09-21', '2021-09-21');
    // console.log(res);

    // 1.获取用户输入的时间段信息
    var inputDate = prompt('请输入您要计算步数的时间段 注意输入格式：2021-09-01,2021-09-07'); // '2021-09-01,2021-09-07'

    // 2.根据该时间段分析出开始时间和结束时间
    var startTime = inputDate.split(',')[0]; // '2021-09-01'
    var endTime = inputDate.split(',')[1]; // '2021-09-07'

    // 3.遍历steps数组求出开始时间和结束时间所对应数据的索引号
    function searchIndex(appointTime) {
      for (let i = 0; i < steps.length; i++) {
        if (steps[i]['date'] === appointTime) {
          return i;
        }
      }
    }

    // 4.根据封装的函数求得开始时间和结束时间所对应数据的索引号
    var startIndex = searchIndex(startTime);
    var endIndex = searchIndex(endTime);

    // 5.封装运动步数求和的函数
    function getStepSum(s, e) {
      var stepSum = 0;
      for (let i = s; i <= e; i++) {
        stepSum += steps[i]['step'];
      }
      return stepSum;
    }

    // 6.将第四步求到的索引号代入到步数求和的函数中返回结果
    document.write(`<div>您在${startTime}至${endTime}期间，一共运动了${getStepSum(startIndex, endIndex)}步数</div>`);
  </script>
</body>

</html>